package org.example.service;

import org.example.mapper.UserMapper;
import org.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.chrono.HijrahDate;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public User getUserByName(String username,String password) {
        if(!StringUtils.hasText(username)|| !StringUtils.hasText(password)) {
            return null;
        }
        return userMapper.queryByName(username,password);
    }

    public Integer addUser(User user) {
        if(user.getRealname()==null||user.getRealname().equals("")) {
            return -1;
        }
        return userMapper.insert(user);
    }

    public List<User> queryAll() {
        List<User> users = userMapper.queryAll();
        if(users==null||users.isEmpty()) {
            return null;
        }
        return users;
    }

    public User getUserById(Integer id) {
        if(id==null||id==0) {
            return null;
        }
        User user=userMapper.queryById(id);
        return user;
    }

    public Integer updateById(Integer id) {
        if(id==null||id==0) {
            return -1;
        }
        return userMapper.update(id);
    }

    public String deleteUserById(Integer id) {
        if(id==null||id==0) {
            return "参数为空";
        }
        Integer count=userMapper.delete(id);
        if(count==0) {
            return "删除失败";
        }
        return "删除成功";
    }
}
